/* I (David P Shenba) license this file to you under the Apache License, 
 * Version 2.0 (the "License"); you may not use this 
 * file except in compliance with the License.  You may 
 * obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.shenba.forms;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.collections.Factory;
import org.apache.commons.collections.ListUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.shenba.beans.InnerBean;
import org.shenba.beans.OuterBean;

/**
 * @author shenba
 *
 */
public class DemoForm extends ActionForm {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private List<OuterBean> outerBeans;
	/**
	 * @return the outerBeans
	 */
	public List<OuterBean> getOuterBeans() {
		return outerBeans;
	}
	/**
	 * @param outerBeans the outerBeans to set
	 */
	public void setOuterBeans(List<OuterBean> outerBeans) {
		this.outerBeans = outerBeans;
	}
	/* (non-Javadoc)
	 * @see org.apache.struts.action.ActionForm#reset(org.apache.struts.action.ActionMapping, javax.servlet.http.HttpServletRequest)
	 */
	@SuppressWarnings("unchecked")
	public void reset(ActionMapping actionMapping, HttpServletRequest httpServletRequest) {
		outerBeans = ListUtils.lazyList(new ArrayList<OuterBean>(), new Factory() {
			@Override
			public Object create() {
				return prepareBean();
			}
		});
	}
	/**
	 * this method is where the lazy list for inner bean is intialized
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private OuterBean prepareBean() {
		OuterBean ob = new OuterBean();
		List<InnerBean> innerBeans = ListUtils.lazyList(new ArrayList<InnerBean>(), new Factory() {
	        public Object create() {
	            return new InnerBean();
	        }
	    });
		ob.setInnerBeans(innerBeans);
		return ob;
	}
}
